package com.huafeng.controller;

import com.huafeng.annotations.Log;
import com.huafeng.commonutil.CheckUtils;
import com.huafeng.model.base.Result;
import com.huafeng.model.entity.SysMenu;
import com.huafeng.model.enums.OperationTypeEnum;
import com.huafeng.model.vo.AssignMenuVo;
import com.huafeng.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 花风
 * @date 2023/8/16 21:25
 * @Description SysMenuController
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("admin/system/sysMenu")
public class SysMenuController {
    @Resource
    private SysMenuService sysMenuService;

    @Log(model = "菜单管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("获取菜单列表")
    @GetMapping("findNodes")
    public Result findNodes(){
        return sysMenuService.findNodes();
    }

    @Log(model = "菜单管理",operationType = OperationTypeEnum.INSERT)
    @ApiOperation("新增菜单")
    @PostMapping("save")
    public Result save(@RequestBody SysMenu sysMenu){
        return sysMenuService.insertMenu(sysMenu);
    }

    @Log(model = "菜单管理",operationType = OperationTypeEnum.UPDATE)
    @ApiOperation("修改菜单")
    @PutMapping("update")
    public Result update(@RequestBody SysMenu sysMenu){
        boolean update = sysMenuService.updateById(sysMenu);
        return CheckUtils.check(update);
    }

    @Log(model = "菜单管理",operationType = OperationTypeEnum.DELETE)
    @ApiOperation("删除菜单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        Assert.isTrue(id != null && id > 0,"menuId must not be null and greater than zero.");
        boolean remove = sysMenuService.removeById(id);
        return CheckUtils.check(remove);
    }

    @Log(model = "菜单管理",operationType = OperationTypeEnum.ASSIGN)
    @ApiOperation("根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId){
        Assert.isTrue(roleId != null && roleId > 0,"roleId must not be null and greater than zero.");
        return sysMenuService.findMenuByRoleId(roleId);
    }

    @Log(model = "菜单管理",operationType = OperationTypeEnum.ASSIGN)
    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignMenuVo assignMenuVo) {
        return sysMenuService.doAssign(assignMenuVo);
    }
}
